package com.donger.business.cms.controller;



import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import lombok.AllArgsConstructor;
import com.donger.business.cms.entity.CmsMenus;
import com.donger.business.cms.service.CmsMenusService;

import java.util.List;
import java.util.stream.Collectors;


/**
 * cms菜单表(CmsMenus)表控制层
 *
 * @author somliy
 * @since 2020-05-07 19:05:29
 */
@RestController
@RequestMapping("/cmsMenus")
@AllArgsConstructor
@Slf4j
public class CmsMenusController {
    /**
     * 服务对象
     */
    private final CmsMenusService cmsMenusService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param cmsMenus 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    public Result listPageAll(Page<CmsMenus> page, CmsMenus cmsMenus) {
        return Res.ok(cmsMenusService.page(page, new QueryWrapper<>(cmsMenus)));
    }
    
     /**
     * 查询所有数据
     *
     * @param cmsMenus 查询实体
     * @return 所有数据
     */
    @GetMapping("/list")
    public Result listAll(CmsMenus cmsMenus) {
        return Res.ok(cmsMenusService.list(new QueryWrapper<>(cmsMenus)));
    }
    
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public Result selectOne(@PathVariable("id") Long id) {
        return Res.ok(cmsMenusService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param cmsMenus 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    public Result insert(@RequestBody CmsMenus cmsMenus) {
        return Res.ok(cmsMenusService.save(cmsMenus));
    }

    /**
     * 修改数据
     *
     * @param cmsMenus 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public Result update(@RequestBody CmsMenus cmsMenus) {
        return Res.ok(cmsMenusService.updateById(cmsMenus));
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Res.ok(cmsMenusService.removeById(id));
    }

    /**
     * 查询所有
     */
    @GetMapping("tree")
    public Result<List<CmsMenus>> listResult(){
        List<CmsMenus> cmsMenusList = cmsMenusService.list();
        return Res.ok(recursiveBuildTree(cmsMenusList,0L));
    }

    /**
     * 选择上级菜单
     */
    @GetMapping("/select")
    public Result select() {
        List<CmsMenus> cmsMenusList = cmsMenusService.list();
        CmsMenus cmsMenus = new CmsMenus();
        cmsMenus.setId(0L);
        cmsMenus.setName("一级菜单");
        cmsMenus.setParentId(-1L);
        cmsMenusList.add(cmsMenus);
        return Res.ok(recursiveBuildTree(cmsMenusList,-1L));
    }

    /**
     * 递归建树
     * 不包含根节点
     */
    private List<CmsMenus> recursiveBuildTree(List<CmsMenus> cmsMenusList, Long parentId){
        List<CmsMenus> childList = cmsMenusList.stream().filter(o->o.getParentId().equals(parentId)).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(childList)){
            return null;
        }
        for (CmsMenus cmsMenus : childList){
            cmsMenus.setChildren(recursiveBuildTree(cmsMenusList,cmsMenus.getId()));
        }
        return childList;
    }

}